package com.howells.time.module.main.presenter;

import android.os.Environment;

import com.howells.time.base.http.HttpURL;
import com.howells.time.base.http.HttpUtils;
import com.howells.time.base.http.JsonCallback;
import com.howells.time.module.main.view.MainView;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2016.4.8.
 */
public class MainPresenterImp implements MainPresenter {
    private MainView mainView;

    public MainPresenterImp(MainView mainView) {
        this.mainView = mainView;
    }

    @Override
    public void getUserInfo() {
        HttpUtils.Get(HttpURL.GetInfoApi.toString(), new JsonCallback() {
            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                mainView.resultMsg("网络异常");
            }

            @Override
            public void onResponse(Map<String, Object> response, int id) {
                if(response.get("message").equals("success")){
                    mainView.saveInfo((Map<String, Object>) response.get("data"));
                }else {
                    Logger.d(response);
                    mainView.resultMsg("获取异常");
                }
            }
        });
    }

    @Override
    public void getHead(String id) {
        Logger.d(HttpURL.GetImageApi.toString() +"&logoId="+ id);
        HttpUtils.Get(HttpURL.GetImageApi.toString() +"logoId=" + id, new FileCallBack(Environment.getExternalStorageDirectory() + "/capsule/","touxiang.png") {
            @Override
            public void inProgress(float progress, long total,int id) {

            }

            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                mainView.resultMsg("网络异常");
            }

            @Override
            public void onResponse(File response,int id) {
                Logger.d(response);
                if(response!=null){
                    mainView.saveHeadImage(response.toString());
                }else {
                    Logger.d(response);
                    mainView.resultMsg("头像下载异常");
                }
            }
        });
    }
}
